from rest_framework.views import APIView
from rest_framework.response import Response
from django.core import serializers
import json

# 查询考试科目
from users.models import ClassModel, CourseModel, ExamTaoModel, ChooseModel, JudgeModel, ReadingModel, \
    StudentResultsModel


# 获取题目id
class ExamView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            number = request.POST['number']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            class_num = number[0:10]
            c = ClassModel.objects.filter(number=class_num)
            c1 = json.loads(serializers.serialize("json", c))[0]
            course = CourseModel.objects.filter(class_table=c1['pk'])
            course_json = json.loads(serializers.serialize("json", course))
            data_list = []
            for i in course_json:
                exam_tao = ExamTaoModel.objects.filter(course_id=i['pk'])
                exam_tao_json = json.loads(serializers.serialize("json", exam_tao))
                if len(exam_tao_json) == 0:
                    continue
                else:
                    for i in exam_tao_json:
                        exam_tao = {}
                        exam_tao['exam_tao_id'] = i['pk']
                        exam_tao['exam_tao_name'] = i['fields']['tao_name']
                        data_list.append(exam_tao)
            data = {
                'code': 4000,
                'msg': '参数有误',
                'data': data_list
            }
            return Response(data)


# 查询题目详情
class ExamDetailsView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            id = request.POST['id']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            data_list = []
            c1 = ChooseModel.objects.filter(exam=id)
            if len(c1) == 0:
                pass
            else:
                c2 = json.loads(serializers.serialize("json", c1))
                choose_list = []
                for i in c2:
                    choose_dict = {}
                    choose_dict['choose_id'] = i['pk']
                    choose_dict['title'] = i['fields']['title']
                    choose_dict['options_a'] = i['fields']['options_a']
                    choose_dict['options_b'] = i['fields']['options_b']
                    choose_dict['options_c'] = i['fields']['options_c']
                    choose_dict['options_d'] = i['fields']['options_d']
                    choose_dict['answer'] = i['fields']['answer']
                    choose_list.append(choose_dict)
                data_list.append(choose_list)
            judge_id = JudgeModel.objects.filter(exam=id)
            if len(judge_id) == 0:
                pass
            else:
                judge_json = json.loads(serializers.serialize("json", judge_id))
                judge_list = []
                for i in judge_json:
                    judge_dict = {}
                    judge_dict['judge_id'] = i['pk']
                    judge_dict['judge_title'] = i['fields']['judge_title']
                    judge_dict['judge_options_Y'] = i['fields']['judge_options_Y']
                    judge_dict['judge_options_N'] = i['fields']['judge_options_N']
                    judge_dict['judge_answer'] = i['fields']['judge_answer']
                    judge_list.append(judge_dict)
                data_list.append(judge_list)
            r1 = ReadingModel.objects.filter(exam=id)
            if len(r1) == 0:
                pass
            else:
                r2 = json.loads(serializers.serialize("json", r1))
                read_list = []
                for i in r2:
                    read_dict = {}
                    read_dict['reading_id'] = i['pk']
                    read_dict['reading_title'] = i['fields']['reading_title']
                    read_dict['reading_answer'] = i['fields']['reading_answer']
                    read_list.append(read_dict)
                data_list.append(read_list)
            data = {
                'code': 2000,
                'msg': '查询成功',
                'data': data_list
            }
            return Response(data)


# 选择题答案校验
class ChooseCheckView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            id = request.POST['id']
            answer = request.POST['answer']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            c1 = ChooseModel.objects.filter(id=id)
            if len(c1) == 0:
                data = {
                    'code': 4000,
                    'msg': '题目不存在',
                }
                return Response(data)
            else:
                c2 = json.loads(serializers.serialize("json", c1))[0]
                answer_s = c2['fields']['answer']
                if answer_s == answer:
                    data = {
                        'code': 2000,
                        'msg': '答案正确',
                        'score': 5
                    }
                    return Response(data)
                else:
                    data = {
                        'code': 4000,
                        'msg': '答案错误',
                        'score': 0,
                        'answer': answer_s
                    }
                    return Response(data)


# 判断题答案校验
class JudgeCheckView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            id = request.POST['id']
            answer = request.POST['answer']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            c1 = JudgeModel.objects.filter(id=id)
            if len(c1) == 0:
                data = {
                    'code': 4000,
                    'msg': '题目不存在',
                }
                return Response(data)
            else:
                c2 = json.loads(serializers.serialize("json", c1))[0]
                answer_s = c2['fields']['judge_answer']
                if answer_s == answer:
                    data = {
                        'code': 2000,
                        'msg': '答案正确',
                        'score': 6
                    }
                    return Response(data)
                else:
                    data = {
                        'code': 4000,
                        'msg': '答案错误',
                        'score': 0,
                        'answer': answer_s
                    }
                    return Response(data)


# 简答题答案校验
class ReadCheckView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            id = request.POST['id']
            answer = request.POST['answer']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            c1 = ReadingModel.objects.filter(id=id)
            if len(c1) == 0:
                data = {
                    'code': 4000,
                    'msg': '题目不存在',
                }
                return Response(data)
            else:
                c2 = json.loads(serializers.serialize("json", c1))[0]
                answer_s = c2['fields']['reading_answer']
                if answer_s == answer:
                    data = {
                        'code': 2000,
                        'msg': '答案正确',
                        'score': 10
                    }
                    return Response(data)
                else:
                    data = {
                        'code': 4000,
                        'msg': '答案错误',
                        'score': 0,
                        'answer': answer_s
                    }
                    return Response(data)


# 提交成绩
class SubmitGradesView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            user_id = request.POST['user_id']
            exam_id = request.POST['exam_id']
            course_id = request.POST['course_id']
            choose = request.POST['choose']
            judge = request.POST['judge']
            read = request.POST['read']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            combined = int(choose) + int(judge) + int(read)
            import datetime
            data_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            StudentResultsModel.objects.create(student=user_id, exam=exam_id, course=course_id, choose=choose,
                                               judge=judge, read=read, combined=combined, data_time=data_time)
            data_dict = {'choose': choose, 'judge': judge, 'read': read, 'combined': combined, 'data_time': data_time}
            data = {
                'code': 4000,
                'msg': '考试完成',
                'data': data_dict
            }
            return Response(data)


# 查询考试记录
class UsreHistoryView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            user_id = request.POST['user_id']
        except:
            data = {
                'code': 4000,
                'msg': '参数有误'
            }
            return Response(data)
        else:
            s = StudentResultsModel.objects.filter(student=user_id)
            if len(s) == 0:
                pass
            else:
                s1 = json.loads(serializers.serialize("json", s))
                s_list = []
                for i in s1:
                    s_dict = {}
                    s_dict['id'] = i['pk']
                    s_dict['choose'] = i['fields']['choose']
                    s_dict['judge'] = i['fields']['judge']
                    s_dict['read'] = i['fields']['read']
                    s_dict['combined'] = i['fields']['combined']
                    s_dict['data_time'] = i['fields']['data_time']
                    c = CourseModel.objects.filter(id=i['fields']['course'])
                    if len(c) == 0:
                        data = {
                            'code': 4000,
                            'msg': '没有找到该课程'
                        }
                        return Response(data)
                    else:
                        c1 = json.loads(serializers.serialize("json", c))[0]
                        s_dict['name'] = c1['fields']['name']
                    s_list.append(s_list)
                data = {
                    'code': 4000,
                    'msg': '考试完成',
                    'data': s_list
                }
                return Response(data)
